/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.stakeholder;

import entity.FnBBranchEntity;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import session.stateless.BranchSessionBean;
import util.entityhelper.FnbBranchState;
import util.enumeration.FnbType;
import util.localization.WebLocalization;

/**
 *
 * @author Amelia
 */
@ManagedBean
@ViewScoped
public class ViewBranchManagedBean implements Serializable {

    @EJB
    private BranchSessionBean branchSessionBean;
    private WebLocalization localization;
    private String preferredLocale;
    private Long stakeholderId;

    /** Creates a new instance of ViewBranchManagedBean */
    public ViewBranchManagedBean() {
    }

    public List<FnbBranchState> getBranchList() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */

        //get the stakeholder id from session
        stakeholderId = (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId");
        if (stakeholderId == null) {
            //Session not save. (redirect Stakeholder to login.
            String nextPage = FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(FacesContext.getCurrentInstance().getApplication().getViewHandler().getActionURL(FacesContext.getCurrentInstance(), "/StakeholderLogin.xhtml"));
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } 
        List<FnbBranchState> branchList = new ArrayList();
        try {
            //getBranchRecord
            List<FnBBranchEntity> tempBranchList = new ArrayList();
            //get the branch associated with the stakeholder taken from the session
            tempBranchList = branchSessionBean.viewAllFnBBranch(stakeholderId);
            System.out.println(tempBranchList.size());
            for (int i = 0; i < tempBranchList.size(); i++) {
                // for initializing the STATE you can use the empty constructor and set what u need
                // or u can use the constructors that has pre-defined input values..
                FnbBranchState localizedState = new FnbBranchState();

                localizedState.setId(tempBranchList.get(i).getId());
                localizedState.setBranchAddress(tempBranchList.get(i).getBranchAddress());
                localizedState.setBranchName(tempBranchList.get(i).getBranchName());
                localizedState.setContactNumber(tempBranchList.get(i).getContactNumber());
                localizedState.setContactPerson(tempBranchList.get(i).getContactPerson());
                localizedState.setEmailAddress(tempBranchList.get(i).getEmailAddress());
                localizedState.setCuisinePref(tempBranchList.get(i).getCuisinePref());
                //localizedState.setStakeholder(null);

                // localized                
                FnbType fnbTypeEnum = tempBranchList.get(i).getFnBType();
                localizedState.setLocalizedFnbType(localization.getLocalizedFnbType(fnbTypeEnum));
                localizedState.setIsHalalEnum(tempBranchList.get(i).getIsHalal());
                // TODO: swee zhi

                localizedState.setOperatingHours(tempBranchList.get(i).getOperatingHours());
                // TODO: swee zhi

                //localizedState.setOperatingHoursRemarks(tempBranchList.get(i).getOperatingHoursRemarks());
                // TODO: swee zhi

                localizedState.setPriceRangeEnum(tempBranchList.get(i).getPriceRange());
                // TODO: swee zhi

                // add to branchList (FnbBranchState)
                branchList.add(localizedState);
                System.out.println(localizedState.getBranchName());
                System.out.println(localizedState.getContactNumber());
                System.out.println(localizedState.getContactPerson());
                System.out.println(localizedState.getIsHalalEnum());
                System.out.println(localizedState.getLocalizedFnbType());
                System.out.println(localizedState.getPriceRangeEnum());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return branchList;
    }

    private SelectItem[] createFilterOptions(String[] data) {
        SelectItem[] options = new SelectItem[data.length + 1];

        options[0] = new SelectItem("", "Select");
        for (int i = 0; i < data.length; i++) {
            options[i + 1] = new SelectItem(data[i], data[i]);
        }

        return options;
    }

    public void view_Edit_Branch(FnbBranchState selectedBranch) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedFnBBranch", branchSessionBean.viewFnBBranch(selectedBranch.getId()));
        //redirecting to view edit branch page
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/stakeholder/EditBranch.xhtml"));
        FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);

    }

    public void view_Delete_Branch(FnbBranchState selectedBranch) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedFnBBranch", branchSessionBean.viewFnBBranch(selectedBranch.getId()));
        //redirecting to view delete branch page
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/stakeholder/DeleteBranch.xhtml"));
        FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
    }
}
